﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.UserCollection", ServerTypeId="{e090781e-8899-4672-9b3d-a78f49fad19d}")]
    public class UserCollection : ClientObjectCollection<User>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public UserCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public User Add(UserCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    if (parameters.LoginName == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.LoginName");
                    }
                    if ((parameters.LoginName != null) && (parameters.LoginName.Length == 0))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.LoginName");
                    }
                    if ((parameters.LoginName != null) && (parameters.LoginName.Length > 0xfb))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.LoginName");
                    }
                    if ((parameters.Title != null) && (parameters.Title.Length > 0xff))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Title");
                    }
                    if ((parameters.Email != null) && (parameters.Email.Length > 0xff))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Email");
                    }
                }
            }
            User user = new User(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            user.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(user.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, user);
            base.Context.AddQuery(query);
            base.AddChild(user);
            user.InitFromCreationInformation(parameters);
            return user;
        }

        [Remote]
        public User AddUser(User user)
        {
            User user2 = new User(base.Context, new ObjectPathMethod(base.Context, base.Path, "AddUser", new object[] { user }));
            user2.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(user2.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, user2);
            base.Context.AddQuery(query);
            base.AddChild(user2);
            return user2;
        }

        [Remote]
        public User GetByEmail(string emailAddress)
        {
            Dictionary<string, User> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient)
            {
                if (emailAddress == null)
                {
                    throw ClientUtility.CreateArgumentNullException("emailAddress");
                }
                if ((emailAddress != null) && (emailAddress.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("emailAddress");
                }
                if ((emailAddress != null) && (emailAddress.Length > 0xff))
                {
                    throw ClientUtility.CreateArgumentException("emailAddress");
                }
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByEmail", out obj2))
            {
                dictionary = (Dictionary<string, User>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, User>();
                base.ObjectData.MethodReturnObjects["GetByEmail"] = dictionary;
            }
            User user = null;
            if (!dictionary.TryGetValue(emailAddress, out user))
            {
                user = new User(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByEmail", new object[] { emailAddress }));
                dictionary[emailAddress] = user;
                ObjectIdentityQuery query = new ObjectIdentityQuery(user.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, user);
                base.Context.AddQuery(query);
            }
            return user;
        }

        [Remote]
        public User GetById(int id)
        {
            return new User(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { id }));
        }

        [Remote]
        public User GetByLoginName(string loginName)
        {
            Dictionary<string, User> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient)
            {
                if (loginName == null)
                {
                    throw ClientUtility.CreateArgumentNullException("loginName");
                }
                if ((loginName != null) && (loginName.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("loginName");
                }
                if ((loginName != null) && (loginName.Length > 0xfb))
                {
                    throw ClientUtility.CreateArgumentException("loginName");
                }
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByLoginName", out obj2))
            {
                dictionary = (Dictionary<string, User>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase);
                base.ObjectData.MethodReturnObjects["GetByLoginName"] = dictionary;
            }
            User user = null;
            if (!dictionary.TryGetValue(loginName, out user))
            {
                user = new User(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByLoginName", new object[] { loginName }));
                dictionary[loginName] = user;
                ObjectIdentityQuery query = new ObjectIdentityQuery(user.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, user);
                base.Context.AddQuery(query);
            }
            return user;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }

        [Remote]
        public void Remove(User user)
        {
            if (base.Context.ValidateOnClient && (user == null))
            {
                throw ClientUtility.CreateArgumentNullException("user");
            }
            ClientAction query = new ClientActionInvokeMethod(this, "Remove", new object[] { user });
            base.Context.AddQuery(query);
            base.RemoveChild(user);
        }
    }
}

